import java.io.*;
class MainProject
{
	public static void main(String args[])
	{
		try
		{
			DecisionTree decisionTree = new DecisionTree();
			String sData = "census-income.data.10000";
			String sTest = "census-income.test.5000";
			String sName = "census-income.names";
			BufferedReader testInput = new BufferedReader(new FileReader(sTest));							
			String str;
			int i,j,k,t;
			int sizeData;
			double d = 0;
			/*********************************处理name文件*****************************/
			k = 0;
			BufferedReader nameInput = new BufferedReader(new FileReader(sName));
			while(!((str = nameInput.readLine())).equals("")) ;
			while((str = nameInput.readLine()) != null)
			{	
				/**忽略ignore这一行*/
				if(!str.equals("instance weight: ignore."))
				{
					i = str.indexOf(": ");
					str = str.substring(i+1);
					while((j = str.indexOf(","))!= -1)
					{
						/**建立属性*/
						Attribute.setName(k,Attribute.getNum(k),str.substring(1,j));
						/**填充属性的值域列表*/
						Attribute.setNum(k,Attribute.getNum(k) + 1);
						str = str.substring(j+1);
					}
					j = str.indexOf(".");
					Attribute.setName(k,Attribute.getNum(k),str.substring(1,j));
					/**识别continuous属性*/
					if(Attribute.getName(k,Attribute.getNum(k)).equals("continuous"))
						Attribute.setIfContinuous(k,true);
					else 
						Attribute.setIfContinuous(k,false);
						Attribute.setNum(k,Attribute.getNum(k) + 1);
					k++;
				}
			}
						
			/****************************task1*****************************************/
			String taskData;
			double maxResult;
			double minResult;
			int maxResultTree;
			int minResultTree;
			int maxTree;
			int minTree;
			int averageTree;
			int sum;
			double averageResult;	
			System.out.println("Task1");
			/***********10%*************************/
			System.out.println("10%:");
			maxResult = 0;
			maxResultTree = 0;
			minResult = 1;
			minResultTree = 0;
			maxTree = 0;
			minTree = 100000;
			averageTree = 0;
			sum = 0;
			averageResult = 0;	
			for(i = 0;i < 6;i++)
			{
				j = 0;
				sizeData = 0;
				taskData = "task1_10%_" + i;
				BufferedReader dataInput = new BufferedReader(new FileReader(sData));
				File file = new File(System.getProperty("user.dir"),taskData);
				PrintStream output = new PrintStream(file);
				while((str = dataInput.readLine()) != null)
				{
					if((i + j) % 10 == 0)
					{
						output.println(str);
						sizeData++;
					}
					j++;
				}
				d = decisionTree.go(taskData,sizeData,sTest,5000,false);
				if(d > maxResult)
				{
					maxResult = d;
					maxResultTree = Node.sum;
				}
				if(d < minResult)
				{
					minResult = d;
					minResultTree = Node.sum;
				}
				if(Node.sum > maxTree)
					maxTree = Node.sum;
				if(Node.sum < minTree)
					minTree = Node.sum;
				averageResult = averageResult + d;
				averageTree = averageTree + Node.sum;
			}
			averageResult = averageResult/6;
			averageTree = averageTree/6;
			System.out.println("Max accuracy : "+maxResult+" TreeSum : "+maxResultTree);
			System.out.println("Min accuracy : "+minResult+" TreeSum : "+minResultTree);
			System.out.println("Average accuracy : "+averageResult);
			System.out.println("Max Tree : " + maxTree);
			System.out.println("Min Tree : " + minTree);
			System.out.println("Average Tree : " + averageTree);
			/***********30%****************************/
			maxResult = 0;
			maxResultTree = 0;
			minResult = 1;
			minResultTree = 0;
			maxTree = 0;
			minTree = 100000;
			averageTree = 0;
			sum = 0;
			averageResult = 0;				
			System.out.println("30%:");
			for(i = 0;i < 6;i++)
			{
				j = 0;
				t = 0;				
				sizeData = 0;
				taskData = "task1_30%_" + i;
				BufferedReader dataInput = new BufferedReader(new FileReader(sData));
				File file = new File(System.getProperty("user.dir"),taskData);
				PrintStream output = new PrintStream(file);
				while((str = dataInput.readLine()) != null)
				{
					if(j > i)
					{
						if(t == 3 || t == 6 || t == 9)
						{
							output.println(str);
							sizeData++;
						}
						else if(t == 10)
							t = 0;
						t++;
					}
					j++;
				}
				d = decisionTree.go(taskData,sizeData,sTest,5000,false);
				if(d > maxResult)
				{
					maxResult = d;
					maxResultTree = Node.sum;
				}
				if(d < minResult)
				{
					minResult = d;
					minResultTree = Node.sum;
				}
				if(Node.sum > maxTree)
					maxTree = Node.sum;
				if(Node.sum < minTree)
					minTree = Node.sum;
				averageResult = averageResult + d;
				averageTree = averageTree + Node.sum;
			}
			averageResult = averageResult/6;
			averageTree = averageTree/6;
			System.out.println("Max accuracy : "+maxResult+" TreeSum : "+maxResultTree);
			System.out.println("Min accuracy : "+minResult+" TreeSum : "+minResultTree);
			System.out.println("Average accuracy : "+averageResult);
			System.out.println("Max Tree : " + maxTree);
			System.out.println("Min Tree : " + minTree);
			System.out.println("Average Tree : " + averageTree);
			/***********60%****************************/
			maxResult = 0;
			maxResultTree = 0;
			minResult = 1;
			minResultTree = 0;
			maxTree = 0;
			minTree = 100000;
			averageTree = 0;
			sum = 0;
			averageResult = 0;					
			System.out.println("60%:");
			for(i = 0;i < 6;i++)
			{
				j = 0;
				t = 0;				
				sizeData = 0;
				taskData = "task1_60%_" + i;
				BufferedReader dataInput = new BufferedReader(new FileReader(sData));
				File file = new File(System.getProperty("user.dir"),taskData);
				PrintStream output = new PrintStream(file);
				while((str = dataInput.readLine()) != null)
				{
					if(j > i)
					{
						if(t == 1 || t == 3 || t == 4 || t == 5|| t == 6 || t == 8)
						{
							output.println(str);
							sizeData++;
						}
						else if(t == 10)
							t = 0;
						t++;
					}
					j++;
				}
				d = decisionTree.go(taskData,sizeData,sTest,5000,false);
				if(d > maxResult)
				{
					maxResult = d;
					maxResultTree = Node.sum;
				}
				if(d < minResult)
				{
					minResult = d;
					minResultTree = Node.sum;
				}
				if(Node.sum > maxTree)
					maxTree = Node.sum;
				if(Node.sum < minTree)
					minTree = Node.sum;
				averageResult = averageResult + d;
				averageTree = averageTree + Node.sum;
			}
			averageResult = averageResult/6;
			averageTree = averageTree/6;
			System.out.println("Max accuracy : "+maxResult+" TreeSum : "+maxResultTree);
			System.out.println("Min accuracy : "+minResult+" TreeSum : "+minResultTree);
			System.out.println("Average accuracy : "+averageResult);
			System.out.println("Max Tree : " + maxTree);
			System.out.println("Min Tree : " + minTree);
			System.out.println("Average Tree : " + averageTree);
			/***********80%****************************/
			maxResult = 0;
			maxResultTree = 0;
			minResult = 1;
			minResultTree = 0;
			maxTree = 0;
			minTree = 100000;
			averageTree = 0;
			sum = 0;
			averageResult = 0;									
			System.out.println("80%:");
			for(i = 0;i < 6;i++)
			{
				j = 0;
				t = 0;				
				sizeData = 0;
				taskData = "task1_80%_" + i;
				BufferedReader dataInput = new BufferedReader(new FileReader(sData));
				File file = new File(System.getProperty("user.dir"),taskData);
				PrintStream output = new PrintStream(file);
				while((str = dataInput.readLine()) != null)
				{
					if(j > i)
					{
						if(t == 1 || t == 2|| t == 3 || t == 4 || t == 5|| t == 6 || t == 7|| t == 8)
						{
							output.println(str);
							sizeData++;
						}
						else if(t == 10)
							t = 0;
						t++;
					}
					j++;
				}
				d = decisionTree.go(taskData,sizeData,sTest,5000,false);
				if(d > maxResult)
				{
					maxResult = d;
					maxResultTree = Node.sum;
				}
				if(d < minResult)
				{
					minResult = d;
					minResultTree = Node.sum;
				}
				if(Node.sum > maxTree)
					maxTree = Node.sum;
				if(Node.sum < minTree)
					minTree = Node.sum;
				averageResult = averageResult + d;
				averageTree = averageTree + Node.sum;
			}
			averageResult = averageResult/6;
			averageTree = averageTree/6;
			System.out.println("Max accuracy : "+maxResult+" TreeSum : "+maxResultTree);
			System.out.println("Min accuracy : "+minResult+" TreeSum : "+minResultTree);
			System.out.println("Average accuracy : "+averageResult);
			System.out.println("Max Tree : " + maxTree);
			System.out.println("Min Tree : " + minTree);
			System.out.println("Average Tree : " + averageTree);
			/****************************task2*****************************************/
			System.out.println("Task2");
			/*******************1:2***************************/
			maxResult = 0;
			maxResultTree = 0;
			minResult = 1;
			minResultTree = 0;
			maxTree = 0;
			minTree = 100000;
			averageTree = 0;
			sum = 0;
			averageResult = 0;					
			System.out.println("1:2:");
			for(i = 0;i < 6;i++)
			{
				j = 0;
				t = 0;
				sizeData = 0;
				taskData = "task2_1:2_" + i;
				BufferedReader dataInput = new BufferedReader(new FileReader(sData));
				File file = new File(System.getProperty("user.dir"),taskData);
				PrintStream output = new PrintStream(file);
				while((str = dataInput.readLine()) != null)
				{
					if(j > i && j <= 2000)
					{
						if(t == 1 || t == 2 || t == 3 || t == 4|| t == 5)
						{
							output.println(str);
							sizeData++;
						}
						else if(t == 6)
							t = 0;
						t++;
					}
					else if( j - 2000 > i)
					{
						if(t == 1 || t == 3 || t == 6 || t == 8|| t == 11)
						{
							output.println(str);
							sizeData++;
						}
						else if(t == 12)
							t = 0;
						t++;
					}
					j++;
				}
				d = decisionTree.go(taskData,sizeData,sTest,5000,false);
				if(d > maxResult)
				{
					maxResult = d;
					maxResultTree = Node.sum;
				}
				if(d < minResult)
				{
					minResult = d;
					minResultTree = Node.sum;
				}
				if(Node.sum > maxTree)
					maxTree = Node.sum;
				if(Node.sum < minTree)
					minTree = Node.sum;
				averageResult = averageResult + d;
				averageTree = averageTree + Node.sum;
			}
			averageResult = averageResult/6;
			averageTree = averageTree/6;
			System.out.println("Max accuracy : "+maxResult+" TreeSum : "+maxResultTree);
			System.out.println("Min accuracy : "+minResult+" TreeSum : "+minResultTree);
			System.out.println("Average accuracy : "+averageResult);
			System.out.println("Max Tree : " + maxTree);
			System.out.println("Min Tree : " + minTree);
			System.out.println("Average Tree : " + averageTree);			
			/*******************1:4***************************/
			maxResult = 0;
			maxResultTree = 0;
			minResult = 1;
			minResultTree = 0;
			maxTree = 0;
			minTree = 100000;
			averageTree = 0;
			sum = 0;
			averageResult = 0;					
			System.out.println("1:4:");
			for(i = 0;i < 6;i++)
			{
				j = 0;
				t = 0;
				sizeData = 0;
				taskData = "task2_1:4_" + i;
				BufferedReader dataInput = new BufferedReader(new FileReader(sData));
				File file = new File(System.getProperty("user.dir"),taskData);
				PrintStream output = new PrintStream(file);
				while((str = dataInput.readLine()) != null)
				{
					if(j > i)
					{
						if(t == 1)
						{
							output.println(str);
							sizeData++;
						}
						else if(t == 2)
							t = 0;
						t++;
					}
					j++;
				}
				d = decisionTree.go(taskData,sizeData,sTest,5000,false);
				if(d > maxResult)
				{
					maxResult = d;
					maxResultTree = Node.sum;
				}
				if(d < minResult)
				{
					minResult = d;
					minResultTree = Node.sum;
				}
				if(Node.sum > maxTree)
					maxTree = Node.sum;
				if(Node.sum < minTree)
					minTree = Node.sum;
				averageResult = averageResult + d;
				averageTree = averageTree + Node.sum;
			}
			averageResult = averageResult/6;
			averageTree = averageTree/6;
			System.out.println("Max accuracy : "+maxResult+" TreeSum : "+maxResultTree);
			System.out.println("Min accuracy : "+minResult+" TreeSum : "+minResultTree);
			System.out.println("Average accuracy : "+averageResult);
			System.out.println("Max Tree : " + maxTree);
			System.out.println("Min Tree : " + minTree);
			System.out.println("Average Tree : " + averageTree);
			/*******************1:6***************************/
			maxResult = 0;
			maxResultTree = 0;
			minResult = 1;
			minResultTree = 0;
			maxTree = 0;
			minTree = 100000;
			averageTree = 0;
			sum = 0;
			averageResult = 0;					
			System.out.println("1:6:");
			for(i = 0;i < 6;i++)
			{
				j = 0;
				t = 0;
				sizeData = 0;
				taskData = "task2_1:6_" + i;
				BufferedReader dataInput = new BufferedReader(new FileReader(sData));
				File file = new File(System.getProperty("user.dir"),taskData);
				PrintStream output = new PrintStream(file);
				while((str = dataInput.readLine()) != null)
				{
					if(j > i && j <= 2000)
					{
						if(t == 1 || t == 3 || t == 6 || t == 9 || t == 12)
						{
							output.println(str);
							sizeData++;
						}
						else if(t == 14)
							t = 0;
						t++;
					}
					else if( j - 2000 > i)
					{
						if(t == 1 || t == 3 || t == 5 || t == 7 || t == 9 || t == 11 || t == 13 || t == 15 || t == 17 || t == 19 ||
						t == 21 || t == 22 || t == 24 || t == 25 || t == 17 )
						{
							output.println(str);
							sizeData++;
						}
						else if(t == 28)
							t = 0;
						t++;
					}
					j++;
				}
				d = decisionTree.go(taskData,sizeData,sTest,5000,false);
				if(d > maxResult)
				{
					maxResult = d;
					maxResultTree = Node.sum;
				}
				if(d < minResult)
				{
					minResult = d;
					minResultTree = Node.sum;
				}
				if(Node.sum > maxTree)
					maxTree = Node.sum;
				if(Node.sum < minTree)
					minTree = Node.sum;
				averageResult = averageResult + d;
				averageTree = averageTree + Node.sum;
			}
			averageResult = averageResult/6;
			averageTree = averageTree/6;
			System.out.println("Max accuracy : "+maxResult+" TreeSum : "+maxResultTree);
			System.out.println("Min accuracy : "+minResult+" TreeSum : "+minResultTree);
			System.out.println("Average accuracy : "+averageResult);
			System.out.println("Max Tree : " + maxTree);
			System.out.println("Min Tree : " + minTree);
			System.out.println("Average Tree : " + averageTree);
			/*******************1:8***************************/
			maxResult = 0;
			maxResultTree = 0;
			minResult = 1;
			minResultTree = 0;
			maxTree = 0;
			minTree = 100000;
			averageTree = 0;
			sum = 0;
			averageResult = 0;					
			System.out.println("1:8:");
			for(i = 0;i < 6;i++)
			{
				j = 0;
				t = 0;
				sizeData = 0;
				taskData = "task2_1:8_" + i;
				BufferedReader dataInput = new BufferedReader(new FileReader(sData));
				File file = new File(System.getProperty("user.dir"),taskData);
				PrintStream output = new PrintStream(file);
				while((str = dataInput.readLine()) != null)
				{
					if(j > i && j <= 2000)
					{
						if(t == 1 || t == 4 || t == 7 || t == 11 || t == 15)
						{
							output.println(str);
							sizeData++;
						}
						else if(t >= 18)
							t = 0;
						t++;
					}
					else if(j - 2000 > i)
					{												
						if(t == 1 || t == 3 || t == 4 || t == 6 || t == 8)
						{
							output.println(str);
							sizeData++;
						}
						else if(t >= 9)
							t = 0;
						t++;
					}
					j++;
				}
				d = decisionTree.go(taskData,sizeData,sTest,5000,false);
				if(d > maxResult)
				{
					maxResult = d;
					maxResultTree = Node.sum;
				}
				if(d < minResult)
				{
					minResult = d;
					minResultTree = Node.sum;
				}
				if(Node.sum > maxTree)
					maxTree = Node.sum;
				if(Node.sum < minTree)
					minTree = Node.sum;
				averageResult = averageResult + d;
				averageTree = averageTree + Node.sum;
			}
			averageResult = averageResult/6;
			averageTree = averageTree/6;
			System.out.println("Max accuracy : "+maxResult+" TreeSum : "+maxResultTree);
			System.out.println("Min accuracy : "+minResult+" TreeSum : "+minResultTree);
			System.out.println("Average accuracy : "+averageResult);
			System.out.println("Max Tree : " + maxTree);
			System.out.println("Min Tree : " + minTree);
			System.out.println("Average Tree : " + averageTree);
			/****************************task3*****************************************/
			System.out.println("Task3");
			/***********10%*************************/
			System.out.println("10%:");
			maxResult = 0;
			maxResultTree = 0;
			minResult = 1;
			minResultTree = 0;
			maxTree = 0;
			minTree = 100000;
			averageTree = 0;
			sum = 0;
			averageResult = 0;	
			for(i = 0;i < 6;i++)
			{
				j = 0;
				sizeData = 0;
				taskData = "task3_10%_" + i;
				BufferedReader dataInput = new BufferedReader(new FileReader(sData));
				File file = new File(System.getProperty("user.dir"),taskData);
				PrintStream output = new PrintStream(file);
				while((str = dataInput.readLine()) != null)
				{
					if((i + j) % 10 == 0)
					{
						output.println(str);
						sizeData++;
					}
					j++;
				}
				d = decisionTree.go(taskData,sizeData,sTest,5000,true);
				if(d > maxResult)
				{
					maxResult = d;
					maxResultTree = Node.sum;
				}
				if(d < minResult)
				{
					minResult = d;
					minResultTree = Node.sum;
				}
				if(Node.sum > maxTree)
					maxTree = Node.sum;
				if(Node.sum < minTree)
					minTree = Node.sum;
				averageResult = averageResult + d;
				averageTree = averageTree + Node.sum;
			}
			averageResult = averageResult/6;
			averageTree = averageTree/6;
			System.out.println("Max accuracy : "+maxResult+" TreeSum : "+maxResultTree);
			System.out.println("Min accuracy : "+minResult+" TreeSum : "+minResultTree);
			System.out.println("Average accuracy : "+averageResult);
			System.out.println("Max Tree : " + maxTree);
			System.out.println("Min Tree : " + minTree);
			System.out.println("Average Tree : " + averageTree);
			/***********30%****************************/
			maxResult = 0;
			maxResultTree = 0;
			minResult = 1;
			minResultTree = 0;
			maxTree = 0;
			minTree = 100000;
			averageTree = 0;
			sum = 0;
			averageResult = 0;				
			System.out.println("30%:");
			for(i = 0;i < 6;i++)
			{
				j = 0;
				t = 0;				
				sizeData = 0;
				taskData = "task3_30%_" + i;
				BufferedReader dataInput = new BufferedReader(new FileReader(sData));
				File file = new File(System.getProperty("user.dir"),taskData);
				PrintStream output = new PrintStream(file);
				while((str = dataInput.readLine()) != null)
				{
					if(j > i)
					{
						if(t == 3 || t == 6 || t == 9)
						{
							output.println(str);
							sizeData++;
						}
						else if(t == 10)
							t = 0;
						t++;
					}
					j++;
				}
				d = decisionTree.go(taskData,sizeData,sTest,5000,true);
				if(d > maxResult)
				{
					maxResult = d;
					maxResultTree = Node.sum;
				}
				if(d < minResult)
				{
					minResult = d;
					minResultTree = Node.sum;
				}
				if(Node.sum > maxTree)
					maxTree = Node.sum;
				if(Node.sum < minTree)
					minTree = Node.sum;
				averageResult = averageResult + d;
				averageTree = averageTree + Node.sum;
			}
			averageResult = averageResult/6;
			averageTree = averageTree/6;
			System.out.println("Max accuracy : "+maxResult+" TreeSum : "+maxResultTree);
			System.out.println("Min accuracy : "+minResult+" TreeSum : "+minResultTree);
			System.out.println("Average accuracy : "+averageResult);
			System.out.println("Max Tree : " + maxTree);
			System.out.println("Min Tree : " + minTree);
			System.out.println("Average Tree : " + averageTree);
			/***********60%****************************/
			maxResult = 0;
			maxResultTree = 0;
			minResult = 1;
			minResultTree = 0;
			maxTree = 0;
			minTree = 100000;
			averageTree = 0;
			sum = 0;
			averageResult = 0;					
			System.out.println("60%:");
			for(i = 0;i < 6;i++)
			{
				j = 0;
				t = 0;				
				sizeData = 0;
				taskData = "task3_60%_" + i;
				BufferedReader dataInput = new BufferedReader(new FileReader(sData));
				File file = new File(System.getProperty("user.dir"),taskData);
				PrintStream output = new PrintStream(file);
				while((str = dataInput.readLine()) != null)
				{
					if(j > i)
					{
						if(t == 1 || t == 3 || t == 4 || t == 5|| t == 6 || t == 8)
						{
							output.println(str);
							sizeData++;
						}
						else if(t == 10)
							t = 0;
						t++;
					}
					j++;
				}
				d = decisionTree.go(taskData,sizeData,sTest,5000,true);
				if(d > maxResult)
				{
					maxResult = d;
					maxResultTree = Node.sum;
				}
				if(d < minResult)
				{
					minResult = d;
					minResultTree = Node.sum;
				}
				if(Node.sum > maxTree)
					maxTree = Node.sum;
				if(Node.sum < minTree)
					minTree = Node.sum;
				averageResult = averageResult + d;
				averageTree = averageTree + Node.sum;
			}
			averageResult = averageResult/6;
			averageTree = averageTree/6;
			System.out.println("Max accuracy : "+maxResult+" TreeSum : "+maxResultTree);
			System.out.println("Min accuracy : "+minResult+" TreeSum : "+minResultTree);
			System.out.println("Average accuracy : "+averageResult);
			System.out.println("Max Tree : " + maxTree);
			System.out.println("Min Tree : " + minTree);
			System.out.println("Average Tree : " + averageTree);			
		} catch(IOException e)
		{
			System.out.println("程序出现IO异常！");
		}
	}
}
